package com.tanhua.server.rmq;


import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Similarity;
import com.tanhua.dubbo.api.QuestionnaireApi;
import com.tanhua.model.db.Extra;
import com.tanhua.model.db.Report;
import com.tanhua.model.db.Similar;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/*
计算相似度的rmq
 */
@Component
public class AnalysisReport {

    @DubboReference
    private QuestionnaireApi questionnaireApi;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(
                    value = "tanhua.log.report"
            ),
            exchange = @Exchange(
                    value = "tanhua.log.exchange",
                    type = ExchangeTypes.TOPIC
            ),
            key = "log.similar.report"
    ))
    public void similar(String message) {
        Map<String, String> map = JSON.parseObject(message, Map.class);
        Long userId = Long.valueOf(map.get("userId"));
        String level = (String) map.get("type");
        //删除自己的有关的所有级别相关的相似度

        //查询所有的报告
        List<Report> reports = questionnaireApi.findReportAll(level);

        //自己的
        Similarity similarity1 = new Similarity();
        //获取到自己的报告
        Report reportUser = questionnaireApi.findByUserIdReport(userId, level);
        String jsonString = JSON.toJSONString(reportUser.getDimensions());
        List<Extra> dimensions = JSON.parseArray(jsonString, Extra.class);
        System.out.println(dimensions);
        for (Extra dimension : dimensions) {
            String replaceAll = dimension.getValueOne().replaceAll("%", "");
            System.out.println(replaceAll);
            Double of = Double.valueOf(replaceAll);
            similarity1.rating_map.put(dimension.getKeyOne(), of/100);
        }

        for (Report report : reports) {
            //判断是否为自己
            if (report.getUserId() != userId) {
                System.out.println(report.getUserId());
                //其他人的
                Similarity similarity2 = new Similarity();

                String jsonString1 = JSON.toJSONString(report.getDimensions());
                List<Extra> ds = JSON.parseArray(jsonString1, Extra.class);
                System.out.println(ds);
                for (Extra d : ds) {
                    String replaceAll = d.getValueOne().replaceAll("%", "");
                    Double of = Double.valueOf(replaceAll);
                    similarity2.rating_map.put(d.getKeyOne(), of/100);
                }
                double v = similarity1.getsimilarity_bydim(similarity2);
                //设置相识度表
                Similar similar = new Similar();
                similar.setUserId(userId);
                similar.setSUserId(report.getUserId());
                similar.setScore(v);
                similar.setLevel(level);
                //添加
                questionnaireApi.saveSimilar(similar);
            }
        }


    }
}
